package com.biz.primus.ms.order.dao.po;

import com.biz.primus.model.order.enums.*;
import com.biz.primus.ms.base.jpa.po.BaseEntity;
import com.biz.primus.ms.order.dao.po.embeddable.OrderConsigneeInfo;
import com.biz.primus.ms.order.dao.po.embeddable.OrderDeliveryInfo;
import com.biz.primus.ms.order.dao.po.embeddable.OrderPromotion;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.envers.NotAudited;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.List;

/**
 * Order
 *
 * @author Tiger Feng
 * @date 2017年10月28日
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "ord_order", indexes = {
        @Index(name = "idx_ord_order_order_code", columnList = "orderCode"),
        @Index(name = "idex_ord_order_depot_ode", columnList = "depotCode")
})
public class Order extends BaseEntity {
    private static final long serialVersionUID = 7681384779621772837L;

    /**
     * 订单明细，商品
     */
    @NotAudited
    @OneToMany(mappedBy = "order", orphanRemoval = true, cascade = CascadeType.ALL)
    private List<OrderItem> items;
    /**
     * 订单类型(闪送、商城)
     */
    @Enumerated(EnumType.STRING)
    @Column(length = 8)
    private OrderType orderType;
    /**
     * 订单编号
     */
    @Column(length = 64, unique = true)
    private String orderCode;
    /**
     * 订单名称
     */
    @Column(length = 64)
    private String orderName;
    /**
     * 用户ID
     */
    @Column(nullable = false)
    private Long userId;
    /**
     * 门店编码(门店或省仓店)
     */
    @Column(length = 64)
    private String depotCode;
    /**
     * 订单IP
     */
    @Column(length = 15)
    private String orderIp;
    /**
     * 商品总价，商品的最终价相加
     */
    @Column(nullable = false)
    private Integer productsAmount;
    /**
     * 商品市场价总价
     */
    private Integer marketPriceTotal;
    /**
     * 商品会员价总价
     */
    private Integer memberPriceTotal;
    /**
     * 电子钱包总价
     */
    private Integer eWalletPriceTotal;
    /**
     * 商品特价总价
     */
    private Integer spcialOfferPriceTotal;
    /**
     * 促销抵扣总金额
     */
    private Integer discountPromotionMoneyTotal;
    /**
     * 优惠券抵扣总金额
     */
    private Integer discountCouponMoneyTotal;
    /**
     * 订单应付=商品总价-优惠券金额+运费
     */
    private Integer payableAmount;
    /**
     * 已付款金额
     */
    private Integer paidAmount;
    /**
     * 支付时间,支付成功后更新此字段
     */
    private Timestamp paymentTimestamp;
    /**
     * 支付方式
     */
    @Convert(converter = OrderPaymentWay.Converter.class)
    private OrderPaymentWay paymentWay;
    /**
     * 支付流水号
     */
    @Column(length = 128)
    private String paidSerialNumber;
    /**
     * 订单包含的商品名称 以,,,做分隔
     */
    private String productNames;
    /**
     * 用户备注
     */
    private String userRemark;
    /**
     * 商户自备注(一般用于客服)
     */
    private String selfRemark;
    /**
     * 收货信息
     */
    @Embedded
    private OrderConsigneeInfo consignee;
    /**
     * 物流信息
     */
    @Embedded
    private OrderDeliveryInfo delivery;
    /**
     * 促销
     */
    @Embedded
    private OrderPromotion promotion;
//	//优惠信息
//	@Embedded
//	@NotAudited
//	private OrderPrivilegeInfo privilege;

    //商品总数量
    @Column(nullable = false)
    private Integer quantity = 0;

    /**
     * 支付状态<p>
     * WAIT_PAY：待支付、
     * PAY_SUCCESS：支付成功、
     * PAY_FAILURE：支付失败、
     * PAY_ON_DELIVERY：货到付款</p>
     */
    @Convert(converter = PaymentState.Converter.class)
    private PaymentState paymentState;
    /**
     * 评价状态<p>
     * WAITING_COMMENTS：待评价、
     * ALREADY_COMMENTS：已评价
     * </p>
     */
    @Convert(converter = CommentState.Converter.class)
    private CommentState commentState;
    /**
     * 订单状态<p>
     * WAIT_PAY：待付款,
     * WAIT_SEND：待发货,
     * WAIT_RECEIVE：待收货,
     * WAIT_EVALUATION：待评价,
     * FINISHED：订单完成,
     * CANCELED：取消,
     * AFTER_SALE：售后中,
     * FINISHED_AFTER_SALE：售后完成
     * </p>
     */
    @Convert(converter = OrderState.Converter.class)
    private OrderState state;
    /**
     * 订单来源<p>
     * web：微商城浏览器、
     * ios：iOS、
     * android：安卓、
     * wechat：微商城、
     * applet：小程序、
     * website：官网、
     * other：其他）
     * </p>
     */
    @Enumerated(EnumType.STRING)
    @Column(length = 32)
    private OrderClient client;

    /**
     * 特殊订单类型（voice_order：语音购物订单）
     */
    @Enumerated(EnumType.STRING)
    private SpecialOrderType specialOrderType;
    /**
     * 是否是秒杀订单,true:是,false:否
     */
    private boolean seckillOrder = false;
    /**
     * 删除标志
     */
    private Boolean isSignDeleted = false;
    /**
     * 是否可退货
     */
    //private boolean returnable = true;
    /**
     * 是否关联了退货单
     */
    private boolean haveReturned = false;
    /**
     * 退货单
     */
    @OneToOne(mappedBy = "order", orphanRemoval = true, cascade = CascadeType.ALL)
    private ReturnOrder returnOrder;

    /**
     * 是门店订单（true：门店订单、false：总店订单）
     */
    private boolean isStoreOrder = true;
    /**
     * 推送中台时间
     */
    private Timestamp send2OMSTimestamp;


}
